﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Inventory;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace TuanHA_Paladin
{
    public partial class Classname
    {
        #region Attackable

        private bool Attackable(WoWUnit target)
        {
            if (!ValidUnit(target))
            {
                return false;
            }

            if (!IsEnemy(target))
            {
                return false;
            }

            if (Invulnerable(target))
            {
                return false;
            }

            if (DebuffCCBreakonDamage(target))
            {
                return false;
            }


            return true;
        }

        private bool Attackable(WoWUnit target, int range)
        {
            if (!ValidUnit(target))
            {
                return false;
            }

            if (SpecialUnit.Contains(target.Entry))
            {
                return true;
            }

            if (DebuffCCBreakonDamage(target))
            {
                return false;
            }

            if (range > 5 && !target.InLineOfSpellSight)
            {
                return false;
            }

            if (range <= 5 && !target.IsWithinMeleeRange)
            {
                return false;
            }

            if (range > 5 && target.Distance2D - target.BoundingRadius > range)
            {
                return false;
            }

            if (!IsEnemy(target))
            {
                return false;
            }

            if (Invulnerable(target))
            {
                return false;
            }

            return true;
        }

        //private bool Attackable(WoWUnit target)
        //{
        //    if (ValidUnit(target) && target.Distance < 40 && !Invulnerable(target) && !InvulnerableMagic(target) &&
        //        (DebuffCCBreakonDamage(target) <= 0) && IsEnemy(target) &&
        //        target.InLineOfSpellSight)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        //prevent double ValidUnit Check
        private bool AttackableValid(WoWUnit target)
        {
            if (target.Distance < 40 && !Invulnerable(target) &&
                !DebuffCCBreakonDamage(target) && IsEnemy(target) &&
                target.InLineOfSpellSight)
            {
                return true;
            }
            return false;
        }

        #endregion

        #region  BattleStandard

        public Composite UseBattleStandard()
        {
            return
                new Decorator(
                    ret =>
                    THSettings.Instance.SHealthStone != 0 &&
                    Me.Combat &&
                    Me.HealthPercent < THSettings.Instance.SHealthStone &&
                    Me.CurrentTarget != null &&
                    Me.CurrentTarget.CurrentHealth > Me.HealthPercent &&
                    Me.CurrentMap.IsBattleground,
                    new Action(delegate
                                   {
                                       if (Me.IsAlliance)
                                       {
                                           WoWItem bs = Me.BagItems.FirstOrDefault(o => o.Entry == 18606);
                                           //18606 Alliance Battle Standard

                                           if (!Me.HasAura("Alliance Battle Standard") && bs != null &&
                                               bs.CooldownTimeLeft.TotalMilliseconds <= _myLatency)
                                           {
                                               bs.Use();
                                               //Lua.DoString("RunMacroText(\"/5 Used Healthstone\")");
                                               //Lua.DoString("RunMacroText(\"/s Used Battle Standard\")");
                                               Logging.Write("Use Battle Standard at " + Me.HealthPercent + "%");
                                           }
                                       }

                                       if (Me.IsHorde)
                                       {
                                           WoWItem bs = Me.BagItems.FirstOrDefault(o => o.Entry == 18607);
                                           //18606 Horde Battle Standard

                                           if (!Me.HasAura("Horde Battle Standard") && bs != null &&
                                               bs.CooldownTimeLeft.TotalMilliseconds <= _myLatency)
                                           {
                                               bs.Use();
                                               //Lua.DoString("RunMacroText(\"/5 Used Healthstone\")");
                                               //Lua.DoString("RunMacroText(\"/s Used Battle Standard\")");
                                               Logging.Write("Use Battle Standard at " + Me.HealthPercent + "%");
                                           }
                                       }

                                       return RunStatus.Failure;
                                   })
                    );
        }

        #endregion

        #region CanUseEquippedItem

        //Thank Apoc
        private static bool CanUseEquippedItem(WoWItem item)
        {
            // Check for engineering tinkers!
            var itemSpell = Lua.GetReturnVal<string>("return GetItemSpell(" + item.Entry + ")", 0);
            if (string.IsNullOrEmpty(itemSpell))
                return false;


            return item.Usable && item.Cooldown <= 0;
        }

        #endregion

        #region CastSpell

        private static WoWUnit _lastCastUnit;
        private static DateTime _lastCastTime = DateTime.Now;
        private string _lastCastSpell = "";
        private string _writeReason;

        private void CastSpell(string spellName, WoWUnit u, string reason = "")
        {
            if (u == null || !u.IsValid)
            {
                return;
            }

            //Logging.Write(LogLevel.Diagnostic,
            //              DateTime.Now.ToString("ss:fff") + " Trying to cast: " + spellName + " on " + u);

            //BeaconBuildHolyPower
            if (spellName == "Flash of Light" || spellName == "Divine Light")
            {
                BeaconBuildHolyPower(u);
            }

            //BeaconHealSecondUnit
            if ((spellName == "Eternal Flame" ||
                 spellName == "Holy Light" ||
                 spellName == "Holy Shock" && !IsEnemy(u) ||
                 spellName == "Holy Radiance" ||
                 spellName == "Light of Dawn" ||
                 spellName == "Word of Glory" && !IsEnemy(u)))
            {
                BeaconHealSecondUnit(u);
            }

            while (_gcdReady > DateTime.Now && !NoGCDSpells.Contains(spellName))
            {
            }

            //if (!SpellManager.CanCast(spellName, u))
            //{
            //    Logging.Write("FAIL to Cast " + DateTime.Now.ToString("ss:fff") + " HP: " +
            //                                       Math.Round(Me.HealthPercent) + "% Mana: " +
            //                                       Math.Round(Me.ManaPercent) + " HP: " + Me.CurrentHolyPower + " " +
            //                                       u.Class + " " + Math.Round(u.Distance, 2) + "y " +
            //                                       Math.Round(u.HealthPercent) + "% hp " + spellName);
            //    return;
            //}

            //try
            //{
            SpellManager.Cast(spellName, u);
            //}
            //catch
            //{
            //    Logging.Write("FAIL to Cast " + DateTime.Now.ToString("ss:fff") + " HP: " +
            //                                       Math.Round(Me.HealthPercent) + "% Mana: " +
            //                                       Math.Round(Me.ManaPercent) + " HP: " + Me.CurrentHolyPower + " " +
            //                                       u.Class + " " + Math.Round(u.Distance, 2) + "y " +
            //                                       Math.Round(u.HealthPercent) + "% hp " + spellName);
            //}

            UpdateCooldown(spellName);

            //Prevent spamming
            if (_lastCastUnit != u || spellName != _lastCastSpell ||
                _lastCastTime + TimeSpan.FromMilliseconds(500) < Now)
            {
                string barTwo = "Mana: " + Math.Round(Me.ManaPercent) + " (" + Me.CurrentHolyPower + " hp)";

                string unitLogName;
                if (u == Me)
                {
                    unitLogName = "Myself";
                }
                else if (u.IsPlayer)
                {
                    unitLogName = u.Class.ToString();
                }
                else
                {
                    unitLogName = u.Name;
                }

                _writeReason = "";
                if (reason != "")
                {
                    _writeReason = " (" + reason + ")";
                }

                Color colorlog;
                if (AttackSpells.Contains(spellName))
                {
                    colorlog = Colors.Yellow;
                }

                else if (DefendSpells.Contains(spellName))
                {
                    colorlog = Colors.Green;
                }
                else if (SpecialSpells.Contains(spellName))
                {
                    colorlog = Colors.OrangeRed;
                }
                else
                {
                    colorlog = Colors.GreenYellow;
                }

                Logging.Write(colorlog, DateTime.Now.ToString("ss:fff") + " HP: " +
                                        Math.Round(Me.HealthPercent) + "% " + barTwo + " " +
                                        unitLogName + " " +
                                        Math.Round(u.Distance, 2) + "y " +
                                        Math.Round(u.HealthPercent) + "% hp " + spellName + _writeReason);
            }

            _lastCastTime = DateTime.Now;
            _lastCastSpell = spellName;
            _lastCastUnit = u;

            TickMilisecond = DateTime.Now;

            if (!NoGCDSpells.Contains(spellName))
            {
                UpdateGCD();
            }

            //if (SpellManager.HasSpell("Divine Protection"))
            //{
            //    Logging.Write("SpellManager.HasSpell('Divine Protection')");
            //    Logging.Write("Me.HealthPercent " + Me.HealthPercent);
            //    Logging.Write("THSettings.Instance.SDivineProtection " + THSettings.Instance.SDivineProtection);
            //    Logging.Write("CooldownTimeLeft.TotalMilliseconds " +
            //                  SpellManager.Spells["Divine Protection"].CooldownTimeLeft.TotalMilliseconds);
            //    Logging.Write("Me.HasAura(Divine Shield) " + Me.HasAura("Divine Shield"));
            //}
        }

        #endregion

        #region CanCleanse

        //private bool CanCleanse(WoWAura aura)
        //{
        //    //if (aura == null)
        //    //    return false;

        //    //if (aura.Spell.DispelType == WoWDispelType.Disease || aura.Spell.DispelType == WoWDispelType.Magic ||
        //    //    aura.Spell.DispelType == WoWDispelType.Poison)
        //    if (aura.Spell.DispelType != WoWDispelType.None)
        //    {
        //        //Logging.Write("CanCleanse: " + aura.Name + " - " + aura.SpellId);
        //        return true;
        //    }
        //    return false;
        //}

        #endregion

        #region ConstantFace

        //private void ConstantFace(WoWUnit unit)
        //{
        //    if (!IsOverrideModeOn && !Me.IsSafelyFacing(unit))
        //    {
        //        WoWMovement.ConstantFace(unit.Guid);
        //    }
        //}

        #endregion

        #region CountDebuff

        public static double CountDebuff(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                int numberofDebuff =
                    u.Debuffs.Values.Count(
                        debuff =>
                        (debuff.Spell.DispelType == WoWDispelType.Magic ||
                         debuff.Spell.DispelType == WoWDispelType.Poison ||
                         debuff.Spell.DispelType == WoWDispelType.Disease));

                return numberofDebuff;
            }
        }

        public static double CountDebuffCanCleanse(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                int numberofDebuff =
                    u.Debuffs.Values.Count(debuff => _listAllCanCleanseDebuff.Contains("[" + debuff.SpellId + "]"));
                return numberofDebuff;
            }
        }

        public double CountDebuffRootandSnare(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                //foreach (var Debuff in u.Debuffs)
                //{
                //    if(ListSnare.Contains("[" + Debuff.Value.SpellId + "]") || 
                //        ListRoot.Contains("[" + Debuff.Value.SpellId + "]"))
                //    {
                //        numberofDebuff = numberofDebuff + 1;
                //    }
                //}

                //int numberofDebuff =
                //    u.ActiveAuras.Values.Count(
                //        debuff =>
                //        (ListSnare.Contains("[" + debuff.SpellId + "]") || 
                //        ListRoot.Contains("[" + debuff.SpellId + "]")));
                //        //(debuff.Spell.Mechanic == WoWSpellMechanic.Snared ||
                //         //debuff.Spell.Mechanic == WoWSpellMechanic.Rooted));

                return
                    u.Debuffs.Count(
                        debuff =>
                        _listSnare.Contains("[" + debuff.Value.SpellId + "]") ||
                        _listRoot.Contains("[" + debuff.Value.SpellId + "]"));
            }
        }

        public double CountDebuffRootandSnareMechanic(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                return
                    u.ActiveAuras.Values.Count(
                        debuff =>
                        (debuff.Spell.Mechanic == WoWSpellMechanic.Snared ||
                         debuff.Spell.Mechanic == WoWSpellMechanic.Rooted));
            }
        }

        #endregion

        #region CountDPSTarget

        private int CountDPSTarget(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsArena || Me.CurrentMap.IsBattleground)
                {
                    return NearbyUnFriendlyPlayers.Count(
                        unit =>
                        unit.GotTarget &&
                        unit.CurrentTarget == target &&
                        (TalentSort(unit) >= 2 &&
                         TalentSort(unit) <= 3 &&
                         unit.Location.Distance(target.Location) < 40 ||
                         TalentSort(unit) == 1 &&
                         unit.Location.Distance(target.Location) < 15) &&
                        DebuffCC(unit) <= 0);
                }
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.GotTarget &&
                    unit.CurrentTarget == target &&
                    DebuffCC(unit) <= 0);
            }
        }

        #endregion

        #region CountMagicDPSTarget

        private int CountMagicDPSTarget(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsArena || Me.CurrentMap.IsBattleground)
                {
                    return NearbyUnFriendlyPlayers.Count(
                        unit =>
                        unit.GotTarget &&
                        unit.CurrentTarget == target &&
                        TalentSort(unit) == 3 &&
                        unit.Location.Distance(target.Location) < 40 &&
                        DebuffCC(unit) <= 0);
                }
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.GotTarget &&
                    unit.CurrentTarget == target &&
                    unit.Location.Distance(target.Location) < 40 &&
                    DebuffCC(unit) <= 0);
            }
        }

        #endregion

        #region CountMeleeDPSTarget

        private int CountMeleeDPSTarget(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsArena || Me.CurrentMap.IsBattleground)
                {
                    return NearbyUnFriendlyPlayers.Count(
                        unit =>
                        unit.GotTarget &&
                        unit.CurrentTarget == target &&
                        TalentSort(unit) == 1 &&
                        unit.Location.Distance(target.Location) < 15 &&
                        DebuffCC(unit) <= 0);
                }
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.GotTarget &&
                    unit.CurrentTarget == target &&
                    unit.Location.Distance(target.Location) < 15 &&
                    DebuffCC(unit) <= 0);
            }
        }

        #endregion

        #region CountPhysicDPSTarget

        private int CountPhysicDPSTarget(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (Me.CurrentMap.IsArena || Me.CurrentMap.IsBattleground)
                {
                    return NearbyUnFriendlyPlayers.Count(
                        unit =>
                        unit.GotTarget &&
                        unit.CurrentTarget == target &&
                        (TalentSort(unit) == 2 &&
                         unit.Location.Distance(target.Location) < 40 ||
                         TalentSort(unit) == 1 &&
                         unit.Location.Distance(target.Location) < 25) &&
                        DebuffCC(unit) <= 0);
                }
                return NearbyUnFriendlyUnits.Count(
                    unit =>
                    unit.GotTarget &&
                    unit.CurrentTarget == target &&
                    unit.Location.Distance(target.Location) < 15 &&
                    DebuffCC(unit) <= 0);
            }
        }

        #endregion

        #region CountEneyNeary

        private double CountEneyNeary(WoWObject unitCenter, float distance)
        {
            return
                NearbyUnFriendlyUnits.Where(
                    unit =>
                    unitCenter.Location.Distance(unit.Location) <= distance &&
                    unit.Combat && !unit.IsPet &&
                    (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                     unit.IsTargetingMeOrPet)).Aggregate
                    <WoWUnit, double>(0, (current, unit) => current + 1);
        }

        #endregion

        #region CountEnemyFromPoint

        //private double CountEnemyFromPoint(WoWPoint pointCenter, float radius)
        //{
        //    return
        //        NearbyUnFriendlyUnits.Where(
        //            unit =>
        //            pointCenter.Distance(unit.Location) <= radius &&
        //            unit.Combat).Aggregate
        //            <WoWUnit, double>(0, (current, unit) => current + 1);
        //}

        #endregion

        #region CountEneyTargettingUnit

        private double CountEneyTargettingUnit(WoWUnit target, float distance)
        {
            return
                NearbyUnFriendlyUnits.Where(
                    unit =>
                    GetMinDistance(unit) < distance &&
                    unit.GotTarget &&
                    unit.Level <= target.Level + 3 &&
                    //unit.Combat && 
                    !unit.IsPet &&
                    unit.CurrentTarget == target //&&
                    //DebuffCC(unit) <= MyLatency
                    ).Aggregate
                    <WoWUnit, double>(0, (current, unit) => current + 1);
        }

        #endregion

        #region CountFriendlyNearyNeedHeal

        //private double CountFriendlyNearyNeedHeal(WoWObject unitCenter, float distance, double healthPercnet)
        //{
        //    return
        //        NearbyFriendlyPlayers.Where(
        //            unit =>
        //            unitCenter.Location.Distance(unit.Location) <= distance &&
        //            unit.HealthPercent <= healthPercnet).Aggregate
        //            <WoWUnit, double>(0, (current, unit) => current + 1);
        //}

        #endregion

        #region Debuff

        //public WoWAura CurrentDebuff;

        #region DebuffCC

        private double DebuffCC(WoWUnit target)
        {
            if (target == null || !target.IsValid || !target.IsAlive)
                return 0;

            //using (StyxWoW.Memory.AcquireFrame())
            {
                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListCC.Contains("[" + a.SpellId + "]")).OrderBy(
                //        a => a.TimeLeft).FirstOrDefault();

                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listCC.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        private double DebuffCCCleanseASAP(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListCleanseASAP.Contains("[" + a.SpellId + "]")).OrderBy(
                //        a => a.TimeLeft).
                //           FirstOrDefault();

                //if (longestDebuff == null || !ListCCCanCleanse.Contains("[" + longestDebuff.SpellId + "]"))
                //{
                //    return 0;
                //}

                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listCleanseASAP.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        private static bool DebuffCCBreakonDamage(WoWUnit target)
        {
            if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
            {
                return false;
            }

            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target.GetAuraById(2637) != null ||
                    target.HasAura("Hibernate") ||
                    target.GetAuraById(3355) != null ||
                    target.HasAura("Freezing Trap Effect") ||
                    target.GetAuraById(115750) != null ||
                    target.HasAura("Blinding Light") ||
                    target.GetAuraById(19503) != null ||
                    target.HasAura("Scatter Shot") ||
                    target.GetAuraById(19386) != null ||
                    target.HasAura("Wyvern Sting") ||
                    target.GetAuraById(118) != null ||
                    target.HasAura("Polymorph") ||
                    target.GetAuraById(61305) != null ||
                    target.HasAura("Polymorph: Black Cat") ||
                    target.GetAuraById(28272) != null ||
                    target.HasAura("Polymorph: Pig") ||
                    target.GetAuraById(61721) != null ||
                    target.HasAura("Polymorph: Rabbit") ||
                    target.GetAuraById(61780) != null ||
                    target.HasAura("Polymorph: Turkey") ||
                    target.GetAuraById(28271) != null ||
                    target.HasAura("Polymorph: Turtle") ||
                    target.GetAuraById(20066) != null ||
                    target.HasAura("Repentance") ||
                    target.GetAuraById(2094) != null ||
                    target.HasAura("Blind") ||
                    target.GetAuraById(1776) != null ||
                    target.HasAura("Gouge") ||
                    target.GetAuraById(6770) != null ||
                    target.HasAura("Sap") ||
                    target.GetAuraById(76780) != null ||
                    target.HasAura("Bind Elemental") ||
                    target.GetAuraById(6358) != null ||
                    target.HasAura("Seduction") ||
                    target.GetAuraById(115268) != null ||
                    target.HasAura("Mesmerize")) //Reshape Life
                {
                    return true;
                }

                //if (target == null || !target.IsValid || !target.IsAlive)
                //    return 0;

                //double longestDebuff = 0;

                //foreach (var aura in target.ActiveAuras)
                //{
                //    if (_listCCBreakonDamage.Contains("[" + aura.Value.SpellId + "]") &&
                //        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                //    {
                //        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                //    }
                //}

                //return longestDebuff;
            }
            return false;
        }

        #endregion

        #region DebuffDisarm

        public double DebuffDisarm(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListDisarm.Contains("[" + a.SpellId + "]"))
                //          .OrderBy(a => a.TimeLeft)
                //          .FirstOrDefault();

                //if (longestDebuff == null)
                //{
                //    return 0;
                //}
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listDisarm.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        public double DebuffDisarmCanCleanse(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListDisarm.Contains("[" + a.SpellId + "]"))
                //          .OrderBy(a => a.TimeLeft)
                //          .FirstOrDefault();

                //if (longestDebuff == null || longestDebuff.Spell.DispelType == WoWDispelType.None)
                //{
                //    return 0;
                //}

                //Logging.Write("DebuffDisarmCanCleanse: " + longestDebuff.Name + " - " + longestDebuff.SpellId);
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listDisarm.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.Spell.DispelType == WoWDispelType.Magic &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        #endregion

        #region DebuffRoot

        public double DebuffRoot(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListRoot.Contains("[" + a.SpellId + "]")).OrderBy(a => a.TimeLeft).
                //           FirstOrDefault();

                //if (longestDebuff == null)
                //{
                //    return 0;
                //}
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listRoot.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        public double DebuffRootCanCleanse(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListRoot.Contains("[" + a.SpellId + "]")).OrderBy(a => a.TimeLeft).
                //           FirstOrDefault();

                //if (longestDebuff == null || !ListRootCanCleanse.Contains("[" + longestDebuff.SpellId + "]"))
                //{
                //    return 0;
                //}

                //Logging.Write("DebuffRootCanCleanse: " + longestDebuff.Name + " - " + longestDebuff.SpellId);
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listRootCanCleanse.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        #endregion

        #region DebuffSilence

        public double DebuffSilence(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListSilence.Contains("[" + a.SpellId + "]")).OrderBy(
                //        a => a.TimeLeft)
                //          .FirstOrDefault();

                //if (longestDebuff == null)
                //{
                //    return 0;
                //}
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listSilence.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        public double DebuffSilenceCanCleanse(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListSilence.Contains("[" + a.SpellId + "]")).OrderBy(
                //        a => a.TimeLeft)
                //          .FirstOrDefault();

                //if (longestDebuff == null || !ListSilenceCanCleanse.Contains("[" + longestDebuff.SpellId + "]"))
                //{
                //    return 0;
                //}

                //Logging.Write("DebuffRootCanCleanse: " + longestDebuff.Name + " - " + longestDebuff.SpellId);
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listSilenceCanCleanse.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        #endregion

        #region DebuffSnare

        public double DebuffSnare(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListSnare.Contains("[" + a.SpellId + "]")).OrderBy(a => a.TimeLeft)
                //          .FirstOrDefault();

                //if (longestDebuff == null)
                //{
                //    return 0;
                //}
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listSnare.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        public double DebuffSnareCanCleanse(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                //WoWAura longestDebuff =
                //    target.Debuffs.Values.Where(a => ListSnare.Contains("[" + a.SpellId + "]")).OrderBy(a => a.TimeLeft)
                //          .
                //           FirstOrDefault();

                //if (longestDebuff == null || longestDebuff.Spell.DispelType == WoWDispelType.None)
                //{
                //    return 0;
                //}

                //Logging.Write("DebuffRootCanCleanse: " + longestDebuff.Name + " - " + longestDebuff.SpellId);
                //return longestDebuff.TimeLeft.TotalMilliseconds;
                double longestDebuff = 0;

                foreach (var aura in target.ActiveAuras)
                {
                    if (_listSnareCanCleanse.Contains("[" + aura.Value.SpellId + "]") &&
                        aura.Value.TimeLeft.TotalMilliseconds > longestDebuff)
                    {
                        longestDebuff = aura.Value.TimeLeft.TotalMilliseconds;
                    }
                }

                return longestDebuff;
            }
        }

        #endregion

        #region DebuffDoNotHeal

        private static bool DebuffDoNotHeal(WoWUnit target)
        {
            if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
            {
                return false;
            }

            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target.GetAuraById(605) != null || //Dominate Mind
                    target.HasAura("Dominate Mind") ||
                    target.GetAuraById(33786) != null || //Cyclone
                    target.HasAura("Cyclone") ||
                    //target.GetAuraById(123184) != null || //Dissonance Field
                    //target.HasAura("Dissonance Field") ||
                    target.GetAuraById(121949) != null || //Parasitic Growth
                    target.HasAura("Parasitic Growth") ||
                    target.GetAuraById(113506) != null ||
                    target.GetAuraById(122370) != null ||
                    target.HasAura("Reshape Life")) //Reshape Life
                {
                    return true;
                }

                //return target.ActiveAuras.Any(aura => _listHealDoNot.Contains("[" + aura.Value.SpellId + "]"));
                ////var longestDebuff = target.Debuffs.Values.Any(a => ListCleanseDoNot.Contains("[" + a.SpellId + "]"));
                //return longestDebuff;
            }
            return false;
        }

        #endregion

        #region DebuffDoNotCleanse

        private bool DebuffDoNotCleanse(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return false;

                return target.Debuffs.Values.Any(aura => _listCleanseDoNot.Contains("[" + aura.SpellId + "]"));
                //var longestDebuff = target.Debuffs.Values.Any(a => ListCleanseDoNot.Contains("[" + a.SpellId + "]"));

                //return longestDebuff;
            }
        }

        #endregion

        #region Invulnerable

        private static bool Invulnerable(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
                    return false;

                if (target.GetAuraById(110617) != null || //Deterrence
                    target.HasAura("Deterrence") ||
                    target.GetAuraById(110715) != null || //Dispersion
                    target.HasAura("Dispersion") ||
                    target.GetAuraById(110700) != null || //Divine Shield 
                    target.HasAura("Divine Shield") ||
                    target.GetAuraById(110696) != null || //Ice Block 
                    target.HasAura("Ice Block") ||
                    target.GetAuraById(19263) != null || //Deterrence
                    target.GetAuraById(45438) != null || //Ice Block 
                    target.GetAuraById(642) != null || //Divine Shield
                    target.GetAuraById(47585) != null || //Dispersion 
                    target.GetAuraById(33786) != null || //Cyclone 
                    target.HasAura("Cyclone") ||
                    target.GetAuraById(113506) != null) //Symbiosis: Cyclone 
                {
                    return true;
                }

                return false;

                //return target.ActiveAuras.Any(aura => _listImmune.Contains("[" + aura.Value.SpellId + "]"));
                //bool longestDebuff = target.GetAllAuras().Any(a => ListImmune.Contains("[" + a.SpellId + "]"));
                //return longestDebuff;
            }
        }

        //private bool InvulnerablePhysic(WoWUnit target)
        //{
        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
        //            return false;

        //        if (target.GetAuraById(110617) != null ||//Deterrence
        //            target.GetAuraById(110715) != null ||//Dispersion
        //            target.GetAuraById(110700) != null ||//Divine Shield 
        //            target.GetAuraById(110696) != null ||//Ice Block 
        //            target.GetAuraById(19263) != null ||//Deterrence
        //            target.GetAuraById(45438) != null ||//Ice Block 
        //            target.GetAuraById(642) != null ||//Divine Shield
        //            target.GetAuraById(47585) != null ||//Dispersion 
        //            target.GetAuraById(605) != null ||//Dominate Mind 
        //            target.GetAuraById(33786) != null ||//Cyclone 
        //            target.GetAuraById(113506) != null)//Symbiosis: Cyclone 
        //        {
        //            return true;
        //        }

        //        return false;

        //        //return target.ActiveAuras.Any(aura => _listImmunePhysic.Contains("[" + aura.Value.SpellId + "]"));
        //        //bool longestDebuff = target.GetAllAuras().Any(a => ListImmunePhysic.Contains("[" + a.SpellId + "]"));

        //        //return longestDebuff;
        //    }
        //}

        private static bool InvulnerableMagic(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
                    return false;

                if (target.GetAuraById(110570) != null || //Anti-Magic Shell 
                    target.HasAura("Anti-Magic Shell") ||
                    target.GetAuraById(110788) != null || //Cloak of Shadows 
                    target.HasAura("Cloak of Shadows") ||
                    target.GetAuraById(113002) != null || //Spell Reflection  
                    target.HasAura("Spell Reflection") ||
                    target.GetAuraById(115760) != null || //Glyph of Ice Block 
                    target.HasAura("Glyph of Ice Block") ||
                    target.GetAuraById(131523) != null || //Zen Meditation
                    target.HasAura("Zen Meditation") ||
                    target.GetAuraById(114239) != null || //Phantasm 
                    target.HasAura("Phantasm") ||
                    target.GetAuraById(31224) != null || //Cloak of Shadows
                    target.GetAuraById(8178) != null || //Grounding Totem Effect 
                    target.HasAura("Grounding Totem Effect") ||
                    target.GetAuraById(23920) != null || //Spell Reflection 
                    target.GetAuraById(114028) != null ||
                    target.HasAura("Mass Spell Reflection")) //Mass Spell Reflection 
                {
                    return true;
                }

                return false;

                //return target.ActiveAuras.Any(aura => _listImmuneSpell.Contains("[" + aura.Value.SpellId + "]"));
                //bool longestDebuff = target.GetAllAuras().Any(a => ListImmuneSpell.Contains("[" + a.SpellId + "]"));
                //return longestDebuff;
            }
        }

        private static bool InvulnerableStun(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive || target.IsDead)
                    return false;

                if (target.GetAuraById(46924) != null || //Bladestorm 
                    target.HasAura("Bladestorm") ||
                    target.GetAuraById(48792) != null ||
                    target.HasAura("Icebound Fortitude Reflection")) //Icebound Fortitude Reflection 
                {
                    return true;
                }

                return false;

                //return target.ActiveAuras.Any(aura => _listImmuneStun.Contains("[" + aura.Value.SpellId + "]"));
                //bool longestDebuff = target.GetAllAuras().Any(a => ListImmuneStun.Contains("[" + a.SpellId + "]"));
                //return longestDebuff;
            }
        }

        #endregion

        #endregion

        #region DoT

        #region DotAll

        public double DotAll(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid || !target.IsAlive)
                    return 0;

                WoWAura longestDebuff =
                    target.GetAllAuras().Where(a => _listDot.Contains("[" + a.SpellId + "]")).OrderBy(
                        a => a.TimeLeft).FirstOrDefault();

                if (longestDebuff == null)
                {
                    return 0;
                }
                return longestDebuff.TimeLeft.TotalMilliseconds;
            }
        }

        #endregion

        //#region DoTMagic

        //public double DoTMagic(WoWUnit target)
        //{
        //    if (target == null || !target.IsValid || !target.IsAlive)
        //        return 0;

        //    const string listDebuff =
        //        "Exorcism_879_ Black Arrow_3674_ Explosive Shot_53301_ Moonfire_8921_ Sunfire_93402_ Insect Swarm_5570_ Immolation Trap_13795_ Ignite_12654_ Living Bomb_44457_ Fireball_133_ Frostfire Bolt_44614_ Pyroblast!_92315_ Shadow Word: Pain_589_ Vampiric Touch_34914_ Flame Shock_8050_ Searing Flames_62661_ Immolate_348_ Corruption_172_ Seed of Corruption_27243_ Unstable Affliction_30108_ Censure_31803_";
        //    var longestDebuff =
        //        target.GetAllAuras().Where(a => listDebuff.Contains("_" + a.SpellId + "_")).OrderBy(
        //            a => a.TimeLeft).FirstOrDefault();

        //    if (longestDebuff == null)
        //    {
        //        return 0;
        //    }
        //    return longestDebuff.TimeLeft.TotalMilliseconds;
        //}

        //#endregion

        //#region Curse

        //public double DoTCurse(WoWUnit target)
        //{
        //    if (target == null || !target.IsValid || !target.IsAlive)
        //        return 0;

        //    const string listDebuff = "Curse of Agony_68136_";
        //    var longestDebuff =
        //        target.GetAllAuras().Where(a => listDebuff.Contains("_" + a.SpellId + "_")).OrderBy(
        //            a => a.TimeLeft).FirstOrDefault();

        //    if (longestDebuff == null)
        //    {
        //        return 0;
        //    }
        //    return longestDebuff.TimeLeft.TotalMilliseconds;
        //}

        //#endregion

        //#region DoTDisease

        //public double DoTDisease(WoWUnit target)
        //{
        //    if (target == null || !target.IsValid || !target.IsAlive)
        //        return 0;

        //    const string listDebuff =
        //        "Icy Touch_45477_ Blood Plague_55078_ Frost Fever_55095_ Unholy Blight_49194_ Devouring Plague_2944_";
        //    var longestDebuff =
        //        target.GetAllAuras().Where(a => listDebuff.Contains("_" + a.SpellId + "_")).OrderBy(
        //            a => a.TimeLeft).FirstOrDefault();

        //    if (longestDebuff == null)
        //    {
        //        return 0;
        //    }
        //    return longestDebuff.TimeLeft.TotalMilliseconds;
        //}

        //#endregion

        //#region DoTPhysic

        //public double DoTPhysic(WoWUnit target)
        //{
        //    if (target == null || !target.IsValid || !target.IsAlive)
        //        return 0;

        //    const string listDebuff =
        //        "Rake_1822_ Pounce Bleed_9007_ Rip_1079_ Garrote_703_ Rupture_1943_ Hemorrhage_89775_ Rend_94009_ Deep Wound_12721_ Piercing Shots_63468_ Lacerate_33745_";
        //    var longestDebuff =
        //        target.GetAllAuras().Where(a => listDebuff.Contains("_" + a.SpellId + "_")).OrderBy(
        //            a => a.TimeLeft).FirstOrDefault();

        //    if (longestDebuff == null)
        //    {
        //        return 0;
        //    }
        //    return longestDebuff.TimeLeft.TotalMilliseconds;
        //}

        //#endregion

        //#region DoTPoison

        //public double DoTPoison(WoWUnit target)
        //{
        //    if (target == null || !target.IsValid || !target.IsAlive)
        //        return 0;

        //    const string listDebuff = "Deadly Poison_2818_ Serpent Sting_1978_ Wyvern Sting_19386_";
        //    var longestDebuff =
        //        target.GetAllAuras().Where(a => listDebuff.Contains("_" + a.SpellId + "_")).OrderBy(
        //            a => a.TimeLeft).FirstOrDefault();

        //    if (longestDebuff == null)
        //    {
        //        return 0;
        //    }
        //    return longestDebuff.TimeLeft.TotalMilliseconds;
        //}

        //#endregion

        #endregion

        #region FacingOverride

        private bool FacingOverride(WoWUnit unit)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (unit == null)
                {
                    return false;
                }

                if (SpecialUnit.Contains(unit.Entry))
                {
                    return true;
                }

                if (Me.IsFacing(unit))
                {
                    return true;
                }

                if (!IsOverrideModeOn && THSettings.Instance.SFace)
                {
                    return true;
                }

                if (TreeRoot.Current.Name == "Questing" ||
                    TreeRoot.Current.Name == "Grind Bot" ||
                    TreeRoot.Current.Name == "BGBuddy" ||
                    TreeRoot.Current.Name == "DungeonBuddy" ||
                    TreeRoot.Current.Name == "Mixed Mode")
                {
                    return true;
                }

                return false;
            }
        }

        #endregion

        #region GatherData

        //private readonly HashSet<int> _spellIDGather = new HashSet<int>
        //    {
        //        11196,
        //    };

        //public List<string> DebuffandCastCollected = new List<string>();

        //private string GetCreateClassName(ulong createdbyguid)
        //{
        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        WoWPlayer unitcreateaura = (from unit in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true)
        //                                    where unit.Guid == createdbyguid
        //                                    select unit).FirstOrDefault();

        //        if (unitcreateaura != null && unitcreateaura.IsPlayer)
        //        {
        //            return unitcreateaura.Class.ToString();
        //        }
        //        if (unitcreateaura != null && unitcreateaura.IsPet)
        //        {
        //            return unitcreateaura.Class.ToString() + " (Pet)";
        //        }
        //    }
        //    return "";
        //}

        //private void GatherData()
        //{
        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        List<WoWUnit> allUnit = ObjectManager.GetObjectsOfType<WoWUnit>(true, true);

        //        foreach (WoWUnit unit in allUnit)
        //        {
        //            foreach (var aura in unit.Debuffs)
        //            {
        //                if (!_spellIDGather.Contains(aura.Value.SpellId) &&
        //                    GetCreateClassName(aura.Value.CreatorGuid) != "")
        //                {
        //                    _spellIDGather.Add(aura.Value.SpellId);
        //                    //Logging.Write(GetCreateClassName(aura.Value.CreatorGuid) + "|" + "Debuff" + "|" + aura.Value.SpellId + "|" + aura.Value.Name + "|" + aura.Value.Spell.DispelType + "|" + aura.Value.Spell.Mechanic + "|" + aura.Value.Spell.BaseCooldown);
        //                    DebuffandCastCollected.Add(GetCreateClassName(aura.Value.CreatorGuid) + "|" + "Debuff" + "|" +
        //                                               aura.Value.SpellId + "|" + aura.Value.Name + "|" +
        //                                               aura.Value.Spell.DispelType + "|" + aura.Value.Spell.Mechanic +
        //                                               "|" + aura.Value.Spell.BaseCooldown);
        //                }
        //            }

        //            if (unit.IsCasting && !_spellIDGather.Contains(unit.CastingSpellId) &&
        //                GetCreateClassName(unit.Guid) != "")
        //            {
        //                _spellIDGather.Add(unit.CastingSpellId);
        //                //Logging.Write(unit.Class.ToString() + "|" + "Casting" + "|" + unit.CastingSpellId + "|" + unit.CastingSpell.Name + "|" +unit.CastingSpell.DispelType + "|" + unit.CastingSpell.Mechanic + "|" + unit.CastingSpell.BaseCooldown);
        //                DebuffandCastCollected.Add(unit.Class.ToString() + "|" + "Casting" + "|" + unit.CastingSpellId +
        //                                           "|" + unit.CastingSpell.Name + "|" + unit.CastingSpell.DispelType +
        //                                           "|" + unit.CastingSpell.Mechanic + "|" +
        //                                           unit.CastingSpell.BaseCooldown);
        //            }

        //            //foreach (var aura in unit.Buffs)
        //            //{
        //            //    if (!_spellIDGather.Contains(aura.Value.SpellId))
        //            //    {
        //            //        _spellIDGather.Add(aura.Value.SpellId);
        //            //        Logging.Write("Buff," + aura.Value.SpellId + "," + aura.Value.Name + "," + aura.Value.Spell.DispelType + "," + aura.Value.Spell.Mechanic + "," + aura.Value.Spell.BaseCooldown);
        //            //    }
        //            //}
        //        }
        //    }
        //}

        #endregion

        #region GCD

        private static DateTime _gcdReady;

        //private void UpdateGCDEvent(object sender, LuaEventArgs raw)
        //{
        //    var args = raw.Args;
        //    var player = Convert.ToString(args[0]);

        //    // Not me ... Im out!
        //    if (player != "player")
        //    {
        //        return;
        //    }
        //    var spellID = Convert.ToInt32(args[4]);
        //    var spellName = WoWSpell.FromId(spellID).Name;

        //}

        public static void UpdateGCD()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {

                {
                }
                if (SpellManager.HasSpell("Word of Glory"))
                {
                    _gcdReady = DateTime.Now + SpellManager.Spells["Word of Glory"].CooldownTimeLeft;
                }
                else
                {
                    _gcdReady = DateTime.Now + SpellManager.GlobalCooldownLeft;
                }
            }
        }

        private static bool GCDL()
        {
            return DateTime.Now + TimeSpan.FromMilliseconds(_myLatency) <= _gcdReady;
        }

        public static bool Casting()
        {
            return Me.IsCasting;
        }


        public static bool CastingorGCDL()
        {
            return Casting() || GCDL();
        }

        #endregion

        #region GetAsyncKeyState

        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);

        #endregion

        #region GetSpellCooldown

        public static TimeSpan GetSpellCooldown(string spell)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                SpellFindResults results;
                if (SpellManager.FindSpell(spell, out results))
                {
                    return results.Override != null
                               ? results.Override.CooldownTimeLeft
                               : results.Original.CooldownTimeLeft;
                }

                return TimeSpan.MaxValue;
            }
        }

        #endregion

        #region GetUnitAuras

        //public double AuraCCDuration;
        //public double AuraCCDurationMax;
        //public WoWAura AuraCC;

        //public double AuraCCBreakonDamageDuration;
        //public double AuraCCBreakonDamageDurationMax;
        //public WoWAura AuraCCBreakonDamage;

        //public double AuraDisarmDuration;
        //public double AuraDisarmDurationMax;
        //public WoWAura AuraDisarm;

        //public double AuraImmuneDuration;
        //public double AuraImmuneDurationMax;
        //public WoWAura AuraImmune;

        //public double AuraImmunePhysicDuration;
        //public double AuraImmunePhysicDurationMax;
        //public WoWAura AuraImmunePhysic;

        //public double AuraImmuneSpellDuration;
        //public double AuraImmuneSpellDurationMax;
        //public WoWAura AuraImmuneSpell;

        //public double AuraRootDuration;
        //public double AuraRootDurationMax;
        //public WoWAura AuraRoot;

        //public double AuraSilenceDuration;
        //public double AuraSilenceDurationMax;
        //public WoWAura AuraSilence;

        //public double AuraSnareDuration;
        //public double AuraSnareDurationMax;
        //public WoWAura AuraSnare;

        //public WoWAura AuraCleanseDoNot;
        //public WoWAura AuraHealDoNot;
        //public int NumberofDebuff;

        //private bool GetUnitAuras(WoWUnit u)
        //{
        //    if (u == null || !u.IsValid || !u.IsAlive)
        //        return false;

        //    AuraCC = null;
        //    AuraCCDuration = 0;
        //    AuraCCDurationMax = 0;

        //    AuraCCBreakonDamage = null;
        //    AuraCCBreakonDamageDuration = 0;
        //    AuraCCBreakonDamageDurationMax = 0;

        //    AuraDisarm = null;
        //    AuraDisarmDuration = 0;
        //    AuraDisarmDurationMax = 0;

        //    AuraImmune = null;
        //    AuraImmuneDuration = 0;
        //    AuraImmuneDurationMax = 0;

        //    AuraImmuneSpell = null;
        //    AuraImmuneSpellDuration = 0;
        //    AuraImmuneSpellDurationMax = 0;

        //    AuraImmunePhysic = null;
        //    AuraImmunePhysicDuration = 0;
        //    AuraImmunePhysicDurationMax = 0;

        //    AuraRoot = null;
        //    AuraRootDuration = 0;
        //    AuraRootDurationMax = 0;

        //    AuraSilence = null;
        //    AuraSilenceDuration = 0;
        //    AuraSilenceDurationMax = 0;

        //    AuraSnare = null;
        //    AuraSnareDuration = 0;
        //    AuraSnareDurationMax = 0;

        //    NumberofDebuff = 0;
        //    AuraCleanseDoNot = null;
        //    AuraHealDoNot = null;

        //    foreach (var aura in u.GetAllAuras())
        //    {
        //        //Count Number of Debuff
        //        if (aura.IsHarmful &&
        //            (aura.Spell.DispelType == WoWDispelType.Disease ||
        //             aura.Spell.DispelType == WoWDispelType.Magic ||
        //             aura.Spell.DispelType == WoWDispelType.Poison))
        //        {
        //            NumberofDebuff = NumberofDebuff + 1;
        //        }

        //        //Find out if AuraCleanseDoNot exits
        //        if (ListCleanseDoNot.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraCleanseDoNot = aura.;
        //        }

        //        //Find out if AuraHealDoNot exits
        //        if (ListHealDoNot.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraHealDoNot = aura.;
        //        }

        //        if (ListCC.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraCCDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraCCDuration > AuraCCDurationMax)
        //            {
        //                AuraCC = aura.;
        //                AuraCCDurationMax = AuraCCDuration;
        //            }
        //        }

        //        if (ListCCBreakonDamage.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraCCBreakonDamageDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraCCBreakonDamageDuration > AuraCCBreakonDamageDurationMax)
        //            {
        //                AuraCCBreakonDamage = aura.;
        //                AuraCCBreakonDamageDurationMax = AuraCCBreakonDamageDuration;
        //            }
        //        }

        //        if (ListDisarm.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraDisarmDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraDisarmDuration > AuraDisarmDurationMax)
        //            {
        //                AuraDisarm = aura.;
        //                AuraDisarmDurationMax = AuraDisarmDuration;
        //            }
        //        }

        //        if (ListImmune.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraImmuneDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraImmuneDuration > AuraImmuneDurationMax)
        //            {
        //                AuraImmune = aura.;
        //                AuraImmuneDurationMax = AuraImmuneDuration;
        //            }
        //        }

        //        if (ListImmuneSpell.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraImmuneSpellDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraImmuneSpellDuration > AuraImmuneSpellDurationMax)
        //            {
        //                AuraImmuneSpell = aura.;
        //                AuraImmuneSpellDurationMax = AuraImmuneSpellDuration;
        //            }
        //        }

        //        if (ListImmunePhysic.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraImmunePhysicDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraImmunePhysicDuration > AuraImmunePhysicDurationMax)
        //            {
        //                AuraImmunePhysic = aura.;
        //                AuraImmunePhysicDurationMax = AuraImmunePhysicDuration;
        //            }
        //        }

        //        if (ListRoot.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraRootDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraRootDuration > AuraRootDurationMax)
        //            {
        //                AuraRoot = aura.;
        //                AuraRootDurationMax = AuraRootDuration;
        //            }
        //        }

        //        if (ListSilence.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraSilenceDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraSilenceDuration > AuraSilenceDurationMax)
        //            {
        //                AuraSilence = aura.;
        //                AuraSilenceDurationMax = AuraSilenceDuration;
        //            }
        //        }

        //        if (ListSnare.Contains("[" + aura.SpellId + "]"))
        //        {
        //            AuraSnareDuration = aura.TimeLeft.TotalMilliseconds;
        //            if (AuraSnareDuration > AuraSnareDurationMax)
        //            {
        //                AuraSnare = aura.;
        //                AuraSnareDurationMax = AuraSnareDuration;
        //            }
        //        }
        //    }
        //    return true;
        //}

        #endregion

        #region GetAllMyAuras

        private DateTime _lastGetAllMyAuras = DateTime.Now;

        private void DumpAuras()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (_lastGetAllMyAuras.AddSeconds(10) < DateTime.Now)
                {
                    int i = 1;
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");
                    foreach (WoWAura aura in Me.GetAllAuras())
                    {
                        Logging.Write(LogLevel.Diagnostic,
                                      i + ". Me.GetAllAuras Name: " + aura.Name + " - SpellId: " + aura.SpellId);
                        i = i + 1;
                    }
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");

                    i = 1;
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");
                    foreach (var aura in Me.Auras)
                    {
                        Logging.Write(LogLevel.Diagnostic,
                                      i + ". Me.Auras - Name: " + aura.Value.Name + " - SpellId: " + aura.Value.SpellId);
                        i = i + 1;
                    }
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");

                    i = 1;
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");
                    foreach (var aura in Me.ActiveAuras)
                    {
                        Logging.Write(LogLevel.Diagnostic,
                                      i + ". Me.ActiveAuras - Name: " + aura.Value.Name + " - SpellId: " +
                                      aura.Value.SpellId);
                        i = i + 1;
                    }
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");

                    i = 1;
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");
                    foreach (var aura in Me.PassiveAuras)
                    {
                        Logging.Write(LogLevel.Diagnostic,
                                      i + ". Me.PassiveAuras - Name: " + aura.Value.Name + " - SpellId: " +
                                      aura.Value.SpellId);
                        i = i + 1;
                    }
                    Logging.Write(LogLevel.Diagnostic, "----------------------------------");

                    _lastGetAllMyAuras = DateTime.Now;
                }
            }
        }

        #endregion

        #region GetMaxDistance

        private static float GetMaxDistance(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target != null)
                {
                    return (float) Math.Max(0f, target.Distance2D - target.BoundingRadius);
                }
                return 0;
            }
        }

        #endregion

        #region GetMinDistance

        private static float GetMinDistance(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target != null)
                {
                    return (float) Math.Max(0f, target.Distance2D - target.BoundingRadius);
                }
                return 123456;
            }
        }

        #endregion

        #region GetMyHandonUnit

        private static bool GetMyHandonUnit(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null || !target.IsValid)
                {
                    return false;
                }

                return target.ActiveAuras.Any(a => a.Value.CreatorGuid == Me.Guid && a.Key.Contains("Hand of"));
            }
        }

        #endregion

        #region GetPlayerGotMyHandofSacrifice

        public WoWUnit PlayerGotMyHandofSacrifice;

        //private bool GetPlayerGotMyHandofSacrifice()
        //{
        //    PlayerGotMyHandofSacrifice = null;
        //    PlayerGotMyHandofSacrifice = (from unit in NearbyUnFriendlyPlayers
        //                                  where MyAura("Hand of Sacrifice", unit)
        //                                  select unit).FirstOrDefault();
        //    return PlayerGotMyHandofSacrifice != null;
        //}

        #endregion

        #region GetUnitConsecration

        private WoWUnit _unitConsecration;

        private bool GetUnitConsecration()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitConsecration = null;
                _unitConsecration = (from unit in NearbyUnFriendlyUnits
                                     orderby CountEneyNeary(unit, 10) descending
                                     where Me.Combat
                                     where Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid
                                     where (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                            unit.IsTargetingMeOrPet)
                                     where GetMinDistance(unit) < 30
                                     select unit).FirstOrDefault();
                return _unitConsecration != null;
            }
        }

        #endregion

        #region GetUnitDispellerAround

        private WoWUnit _unitDispellerAround;

        private bool GetUnitDispellerAround()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitDispellerAround = null;
                _unitDispellerAround = (from unit in NearbyUnFriendlyUnits
                                        orderby unit.Distance ascending
                                        where unit.IsPlayer
                                        where
                                            unit.Class == WoWClass.Hunter || unit.Class == WoWClass.Mage ||
                                            unit.Class == WoWClass.Priest || unit.Class == WoWClass.Shaman
                                        where AttackableValid(unit)
                                        select unit).FirstOrDefault();

                return _unitDispellerAround != null;
            }
        }

        #endregion

        #region GetUnitNear

        private WoWUnit _unitNear;

        private bool GetUnitNear()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitNear = null;
                _unitNear = (from unit in NearbyUnFriendlyUnits
                             orderby unit.Distance ascending
                             where Me.Combat
                             where Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid
                             where (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                    unit.IsTargetingMeOrPet)
                             select unit).FirstOrDefault();
                return _unitNear != null;
            }
        }

        #endregion

        #region GetUnitLowThreat

        private WoWUnit _unitLowThreat;

        private bool GetUnitLowThreat()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitLowThreat = null;
                _unitLowThreat = (from unit in NearbyUnFriendlyUnits
                                  orderby unit.ThreatInfo.RawPercent ascending
                                  where Me.Combat
                                  where Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid
                                  where (unit.IsTargetingMyPartyMember || unit.IsTargetingMyRaidMember ||
                                         unit.IsTargetingMeOrPet)
                                  where unit.IsWithinMeleeRange
                                  select unit).FirstOrDefault();
                return _unitLowThreat != null;
            }
        }

        #endregion

        #region GetUnitStunSurvive

        private WoWUnit _unitCroudControlSurvive;

        private bool GetUnitCroudControlSurvive(float distance)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitCroudControlSurvive = null;
                _unitCroudControlSurvive = (from unit in NearbyUnFriendlyUnits
                                            orderby unit.CurrentHealth descending
                                            where unit.Level <= Me.Level + 3
                                            where unit.Combat && !unit.IsPet
                                            where unit.CurrentTarget == Me
                                            where DebuffCC(unit) <= _myLatency
                                            where !Invulnerable(unit)
                                            where !InvulnerableMagic(unit)
                                            where unit.IsTargetingMeOrPet
                                            where GetMinDistance(unit) < distance
                                            where unit.InLineOfSpellSight
                                            select unit).FirstOrDefault();
                return _unitCroudControlSurvive != null;
            }
        }

        #endregion

        #region GetUnitHaveMySacredShield

        private WoWUnit _unitHaveMySacredShield;

        private bool GetUnitHaveMySacredShield()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitHaveMySacredShield = null;
                _unitHaveMySacredShield = (from unit in FarFriendlyPlayers
                                           where MyAura(20925, unit)
                                           select unit).FirstOrDefault();
                return _unitHaveMySacredShield != null;
            }
        }

        #endregion

        #region GetUnitHammerofWrath

        private WoWUnit _unitHammerofWrath;

        private bool GetUnitHammerofWrath()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _unitHammerofWrath = null;
                _unitHammerofWrath = (from unit in NearbyUnFriendlyPlayers
                                      where unit.HealthPercent < 20
                                      where GetMinDistance(unit) < 30
                                      where !Invulnerable(unit)
                                      where unit.InLineOfSpellSight
                                      where FacingOverride(unit)
                                      select unit).FirstOrDefault();
                return _unitHammerofWrath != null;
            }
        }

        #endregion

        #region Healable

        private bool Healable(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (ValidUnit(u) && u.Distance < 40 && !IsEnemy(u) && !DebuffDoNotHeal(u) && u.InLineOfSpellSight &&
                    !u.Name.Contains("Statue"))
                {
                    return true;
                }
                return false;
            }
        }

        //prevent double ValidUnit Check
        private bool HealableValid(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u != null && u.Distance < 40 && !IsEnemy(u) && !DebuffDoNotHeal(u) && u.InLineOfSpellSight)
                {
                    return true;
                }
                return false;
            }
        }

        //private bool Healable60(WoWUnit u)
        //{
        //    if (ValidUnit(u) && u.Distance < 60 && !IsEnemy(u) && !DebuffDoNotHeal(u) && u.InLineOfSpellSight)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        //prevent double ValidUnit Check
        //private bool Healable60Valid(WoWUnit u)
        //{
        //    if (u.Distance < 60 && !IsEnemy(u) && !DebuffDoNotHeal(u) && u.InLineOfSpellSight)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        #endregion

        #region IndexToKeys

        public Keys KeyTwo;

        private Keys IndexToKeys(int index)
        {
            switch (index)
            {
                case 1:
                    return Keys.A;
                case 2:
                    return Keys.B;
                case 3:
                    return Keys.C;
                case 4:
                    return Keys.D;
                case 5:
                    return Keys.E;
                case 6:
                    return Keys.F;
                case 7:
                    return Keys.G;
                case 8:
                    return Keys.H;
                case 9:
                    return Keys.I;
                case 10:
                    return Keys.J;
                case 11:
                    return Keys.K;
                case 12:
                    return Keys.L;
                case 13:
                    return Keys.M;
                case 14:
                    return Keys.N;
                case 15:
                    return Keys.O;
                case 16:
                    return Keys.P;
                case 17:
                    return Keys.Q;
                case 18:
                    return Keys.R;
                case 19:
                    return Keys.S;
                case 20:
                    return Keys.T;
                case 21:
                    return Keys.U;
                case 22:
                    return Keys.V;
                case 23:
                    return Keys.W;
                case 24:
                    return Keys.X;
                case 25:
                    return Keys.Y;
                case 26:
                    return Keys.Z;
                case 27:
                    return Keys.NumPad1;
                case 28:
                    return Keys.NumPad2;
                case 29:
                    return Keys.NumPad3;
                case 30:
                    return Keys.NumPad4;
                case 31:
                    return Keys.NumPad5;
                case 32:
                    return Keys.NumPad6;
                case 33:
                    return Keys.NumPad7;
                case 34:
                    return Keys.NumPad8;
                case 35:
                    return Keys.NumPad9;
                case 36:
                    return Keys.NumPad0;
                case 37:
                    return Keys.Up;
                case 38:
                    return Keys.Down;
                case 39:
                    return Keys.Left;
                case 40:
                    return Keys.Right;
            }
            return Keys.None;
        }

        #endregion

        #region InterruptCast

        private double InterruptCastNoChannel(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                double timeLeft = 0;

                if (target != null && target.IsCasting &&
                    _listCastSpellInterrupt.Contains("[" + target.CastingSpellId + "]"))
                {
                    timeLeft = target.CurrentCastTimeLeft.TotalMilliseconds;
                }
                return timeLeft;
            }
        }

        //public double OffensiveCastNoChannel(WoWUnit target)
        //{
        //    double timeLeft = 0;

        //    const string spellNoChanneled = "[30451]=Arcane Blast [33786]=Cyclone [421]=Chain Lightning [50796]=Chaos Bolt [339]Entangling Roots [5782]=Fear [133]=Fireball [116]=Frostbolt [44614]=Frostfire Bolt [71521]=Hand of Gul'dan [48181]=Haunt [51514]=Hex [14914]=Holy Fire [348]=Immolate [29722]=Incinerate [51505]=Lava Burst [403=Lightning Bolt [8129]=Mana Burn [8092]=Mind Blast [605]=Mind Control [73510]=Mind Spike [118][28272][61721][61305][61780][28271]=Polymorph [11366]=Pyroblast [2948=]Scorch [5676]=Searing Pain [27243]=Seed of Corruption [686]=Shadow Bolt [585]=Smite [6353]=Soul Fire [2912]=Starfire [78674]=Starsurge [30108]=Unstable Affliction [5176]=Wrath [34914]=Vampiric Touch";

        //    if (target != null && target.IsCasting && spellNoChanneled.Contains("[" + target.CastingSpellId + "]"))
        //    {
        //        timeLeft = target.CurrentCastTimeLeft.TotalMilliseconds;
        //    }
        //    return timeLeft;
        //}

        private double InterruptCastChannel(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                double timeLeft = 0;

                if (target != null && target.IsChanneling &&
                    _listChannelSpellsInterrupt.Contains("[" + target.ChanneledSpell.Id + "]"))
                {
                    timeLeft = 500;
                }
                return timeLeft;
            }
        }

        //public double OffensiveCastChannel(WoWUnit target)
        //{
        //    double timeLeft = 0;
        //    const string spellChanneled = "[689]=Drain Life [879]=Exorcism [1120]=Drain Soul [5143]=Arcane Missiles [15407]=Mind Flay [89420]=Drain Life";

        //    if (target != null && target.IsCasting && spellChanneled.Contains("_" + target.CastingSpellId + "_"))
        //    {
        //        timeLeft = 111;
        //    }
        //    return timeLeft;
        //}

        #endregion

        #region IsHealing

        private static bool IsHealing()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                return Me.IsCasting &&
                       (Me.CastingSpell.Name == "Flash of Light" || Me.CastingSpell.Name == "Divine Light" ||
                        Me.CastingSpell.Name == "Holy Light" || Me.CastingSpell.Name == "Holy Radiance");
            }
        }

        #endregion

        #region IsAttacking

        //private static bool IsAttacking()
        //{
        //    return Me.IsCasting && Me.CastingSpell.Name == "Denounce";
        //}

        #endregion

        #region IsGroupHealingOn

        private bool _groupHealingOn;

        private bool IsGroupHealingOn()
        {
            if (!SpellManager.HasSpell("Holy Insight"))
            {
                //Logging.Write("----------------------------------");
                //Logging.Write("Group Healing Mode Off");
                //Logging.Write("Only Activated in Holy Specialization");
                //Logging.Write("----------------------------------");
                _groupHealingOn = false;
                return false;
            }

            if (THSettings.Instance.SGroup1 &&
                THSettings.Instance.SGroup2 &&
                THSettings.Instance.SGroup3 &&
                THSettings.Instance.SGroup4 &&
                THSettings.Instance.SGroup5 &&
                THSettings.Instance.SGroup6 &&
                THSettings.Instance.SGroup7 &&
                THSettings.Instance.SGroup8
                )
            {
                //Logging.Write("----------------------------------");
                //Logging.Write("Group Healing Mode Off");
                //Logging.Write("All Group Selected");
                //Logging.Write("----------------------------------");
                _groupHealingOn = false;
                return false;
            }

            if (!THSettings.Instance.SGroup1 &&
                !THSettings.Instance.SGroup2 &&
                !THSettings.Instance.SGroup3 &&
                !THSettings.Instance.SGroup4 &&
                !THSettings.Instance.SGroup5 &&
                !THSettings.Instance.SGroup6 &&
                !THSettings.Instance.SGroup7 &&
                !THSettings.Instance.SGroup8
                )
            {
                //Logging.Write("----------------------------------");
                //Logging.Write("Selective Group Healing Mode Off");
                //Logging.Write("Not a Single Group Selected");
                //Logging.Write("----------------------------------");
                _groupHealingOn = false;
                return false;
            }

            if (!Me.IsInMyRaid)
            {
                //Logging.Write("----------------------------------");
                //Logging.Write("Selective Group Healing Mode Off");
                //Logging.Write("You are not in Raid");
                //Logging.Write("----------------------------------");
                _groupHealingOn = false;
                return false;
            }

            if (Me.CurrentMap.IsBattleground)
            {
                //Logging.Write("----------------------------------");
                //Logging.Write("Selective Group Healing Mode Off");
                //Logging.Write("You are in Battleground");
                //Logging.Write("----------------------------------");

                //Logging.Write("Me.CurrentMap: " + Me.CurrentMap);
                //Logging.Write("Me.CurrentMap.IsArena: " + Me.CurrentMap.IsArena);
                //Logging.Write("Me.CurrentMap.IsBattleground: " + Me.CurrentMap.IsBattleground);
                //Logging.Write("Me.CurrentMap.IsDungeon: " + Me.CurrentMap.IsDungeon);
                //Logging.Write("Me.CurrentMap.IsRaid: " + Me.CurrentMap.IsRaid);
                //Logging.Write("Me.CurrentMap.IsScenario: " + Me.CurrentMap.IsScenario);
                _groupHealingOn = false;
                return false;
            }

            Logging.Write("----------------------------------");
            Logging.Write("Selective Group Healing Mode On");
            Logging.Write("----------------------------------");
            _groupHealingOn = true;
            return true;
        }

        #endregion

        #region IsEnemy

        public WoWUnit MyPartyorRaidUnit;

        private bool IsMyPartyRaidMember(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid)
                {
                    return false;
                }

                if (_groupHealingOn && !GroupHealingMembers.Contains(u))
                {
                    return false;
                }

                if (!u.IsPlayer)
                {
                    if (Me.PartyMembers.Contains(u.CreatedByUnit) || Me.RaidMembers.Contains(u.CreatedByUnit))
                    {
                        return true;
                    }
                }
                else
                {
                    if (Me.PartyMembers.Contains(u) || Me.RaidMembers.Contains(u))
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        //private bool UnitIsInMyPartyOrRaid(WoWUnit target)
        //{
        //    //using (StyxWoW.Memory.AcquireFrame())
        //    {
        //        MyPartyorRaidUnit = (from unit in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true)
        //                             where unit.IsInMyPartyOrRaid
        //                             where unit.Guid == target.Guid || unit.Guid == target.CreatedByUnitGuid
        //                             select unit).FirstOrDefault();
        //    }
        //    return MyPartyorRaidUnit != null;
        //}

        private bool IsEnemy(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null)
                {
                    return false;
                }

                if (target.Entry == 62442 && target.GotTarget && !target.IsTargetingMyRaidMember) //tsulong
                {
                    return false;
                }

                if (IsMyPartyRaidMember(target))
                {
                    return false;
                }

                if (Me.CurrentMap.IsArena || Me.CurrentMap.IsBattleground)
                {
                    return true;
                }

                if (!target.IsFriendly || target.Name.Contains("Dummy") && Me.Combat && Me.IsFacing(target))
                {
                    return true;
                }
                return false;
            }
        }

        #endregion

        #region IsMeorFocus

        //private bool IsMeorFocus(WoWUnit target)
        //{
        //    if (target == Me ||
        //        target == Me.FocusedUnit && ValidUnit(Me.FocusedUnit) && !IsEnemy(Me.FocusedUnit) ||
        //        target == Me.CurrentTarget && ValidUnit(Me.CurrentTarget) && !IsEnemy(Me.CurrentTarget) ||
        //        Me.CurrentMap.IsArena)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        #endregion

        #region MeIsTank

        public static bool MeIsTank
        {
            get
            {
                //return (StyxWoW.Me.Role & WoWPartyMember.GroupRole.Tank) != 0 ||
                //        Tanks.All(t => !t.IsAlive) && StyxWoW.Me.HasAura("Bear Form");
                return (StyxWoW.Me.Role & WoWPartyMember.GroupRole.Tank) != 0;
            }
        }

        #endregion

        #region MyAura

        private static bool MyAura(string auraName, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return false;

                bool aura = u.GetAllAuras().Any(a => a.Name == auraName && a.CreatorGuid == Me.Guid);

                return aura;
                //return u.ActiveAuras.Any(a => a.Value.Name == auraName && a.Value.CreatorGuid == Me.Guid);
            }
        }


        private static bool MyAura(int auraID, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return false;

                bool aura = u.GetAllAuras().Any(a => a.SpellId == auraID && a.CreatorGuid == Me.Guid);

                return aura;

                //return u.ActiveAuras.Any(a => a.Value.SpellId == auraID && a.Value.CreatorGuid == Me.Guid);
            }
        }

        #endregion

        #region MyAuraTimeLeft

        private static double MyAuraTimeLeft(string auraName, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.Name == auraName && a.CreatorGuid == Me.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.TimeLeft.TotalMilliseconds;
            }
        }

        private static double MyAuraTimeLeft(int auraID, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.SpellId == auraID && a.CreatorGuid == Me.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.TimeLeft.TotalMilliseconds;
            }
        }

        #endregion

        #region LogTest

        //private Composite LogTest(string message)
        //{
        //    return new Action(delegate
        //                          {
        //                              Logging.Write(LogLevel.Diagnostic, "LogTest: " + message);
        //                              return RunStatus.Failure;
        //                          });
        //}

        #endregion

        #region UnitAuraTimeLeft

        private static double UnitAuraTimeLeft(string auraName, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.Name == auraName && a.CreatorGuid == u.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.TimeLeft.TotalMilliseconds;
            }
        }

        public double UnitAuraTimeLeft(int auraID, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.SpellId == auraID && a.CreatorGuid == u.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.TimeLeft.TotalMilliseconds;
            }
        }

        #endregion

        #region MyAuraStackCount

        public double MyAuraStackCount(string auraName, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.Name == auraName && a.CreatorGuid == u.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.StackCount;
            }
        }

        public double MyAuraStackCount(int auraID, WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.IsAlive)
                    return 0;

                WoWAura aura = u.GetAllAuras().FirstOrDefault(a => a.SpellId == auraID && a.CreatorGuid == u.Guid);

                if (aura == null)
                {
                    return 0;
                }
                return aura.StackCount;
            }
        }

        #endregion

        #region ReadXML

        private static string _listAllCanCleanseDebuff;
        private string _listCC;
        private string _listCCBreakonDamage;
        private string _listCCCanCleanse;
        private string _listCastSpellInterrupt;
        private string _listChannelSpellsInterrupt;
        private string _listCleanseASAP;
        private string _listCleanseDoNot;
        private string _listDisarm;
        private string _listDisarmCanCleanse;
        private string _listDot;
        private string _listDotCanCleanse;
        private string _listHealDoNot;
        private string _listImmune;
        private string _listImmunePhysic;
        private string _listImmuneSpell;
        //private string _listImmuneStun;
        private string _listRoot;
        private string _listRootCanCleanse;
        private string _listSilence;
        private string _listSilenceCanCleanse;
        private string _listSnare;
        private string _listSnareCanCleanse;

        private bool _readNextXMLData;
        private XmlTextReader _xmlReader;

        private void ReadXML()
        {
            #region DataSourceXML

            //Get Cast Spells to Interrupt to String
            _listCastSpellInterrupt = "";
            _xmlReader = new XmlTextReader(THSettings.Instance.PathCastSpells);
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCastSpellInterrupt = _listCastSpellInterrupt + "[" +
                                              _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get Channel Spells to Interrupt to String
            _listChannelSpellsInterrupt = "";
            _xmlReader = new XmlTextReader(THSettings.Instance.PathChannelSpells);
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listChannelSpellsInterrupt =
                        _listChannelSpellsInterrupt + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListCleanseASAP to String
            _listCleanseASAP = "";
            _xmlReader = new XmlTextReader(THSettings.Instance.PathCleanseASAP);
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCleanseASAP =
                        _listCleanseASAP + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListCleanseDoNot to String
            _listCleanseDoNot = "";
            _xmlReader = new XmlTextReader(THSettings.Instance.PathCleanseDoNot);
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCleanseDoNot =
                        _listCleanseDoNot + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListHealDoNot to String
            _listHealDoNot = "";
            _xmlReader = new XmlTextReader(THSettings.Instance.PathHealDoNot);
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listHealDoNot = _listHealDoNot + "[" + _xmlReader.Value + "] ";
                    //ListHealDoNot.Add(XmlReader.Value);
                    //ListCC = XmlReader.Value;
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            #endregion

            #region Debuff XML

            //Get ListCC to String
            _listCC = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListCC.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCC = _listCC + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListCCCanCleanse to String
            _listCCCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListCCCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCCCanCleanse = _listCCCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListCCBreakonDamage to String
            _listCCBreakonDamage = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListCCBreakonDamage.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listCCBreakonDamage = _listCCBreakonDamage + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListDisarm to String
            _listDisarm = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListDisarm.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listDisarm = _listDisarm + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListDisarmCanCleanse to String
            _listDisarmCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListDisarmCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listDisarmCanCleanse = _listDisarmCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListDot to String
            _listDot = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListDot.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listDot = _listDot + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListDotCanCleanse to String
            _listDotCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListDotCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listDotCanCleanse = _listDotCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListImmune to String
            _listImmune = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListImmune.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listImmune = _listImmune + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListImmuneSpell to String
            _listImmuneSpell = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListImmuneSpell.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listImmuneSpell = _listImmuneSpell + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListImmuneStun to String
            //_listImmuneStun = "";
            //_xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
            //                                            string.Format(
            //                                                @"Routines/TuanHAPaladinSpecialEdition/THListImmuneStun.xml")));
            //while (_xmlReader.Read())
            //{
            //    if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
            //    {
            //        _readNextXMLData = true;
            //    }
            //    if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
            //    {
            //        _listImmuneSpell = _listImmuneSpell + "[" + _xmlReader.Value + "] ";
            //        _readNextXMLData = false;
            //    }
            //}
            //_xmlReader.Close();


            //Get ListImmunePhysic to String
            _listImmunePhysic = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListImmunePhysic.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listImmunePhysic = _listImmunePhysic + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListRoot to String
            _listRoot = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListRoot.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listRoot = _listRoot + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListRootCanCleanse to String
            _listRootCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListRootCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listRootCanCleanse = _listRootCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListSilence to String
            _listSilence = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListSilence.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listSilence = _listSilence + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListSilenceCanCleanse to String
            _listSilenceCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListSilenceCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listSilenceCanCleanse = _listSilenceCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListSnare to String
            _listSnare = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListSnare.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listSnare = _listSnare + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            //Get ListSnareCanCleanse to String
            _listSnareCanCleanse = "";
            _xmlReader = new XmlTextReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                        string.Format(
                                                            @"Routines/TuanHAPaladinSpecialEdition/THListSnareCanCleanse.xml")));
            while (_xmlReader.Read())
            {
                if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.Name == "SID")
                {
                    _readNextXMLData = true;
                }
                if (_xmlReader.NodeType == XmlNodeType.Text && _readNextXMLData)
                {
                    _listSnareCanCleanse = _listSnareCanCleanse + "[" + _xmlReader.Value + "] ";
                    _readNextXMLData = false;
                }
            }
            _xmlReader.Close();

            _listAllCanCleanseDebuff = _listDotCanCleanse + _listCCCanCleanse + _listRootCanCleanse +
                                       _listSilenceCanCleanse +
                                       _listSnareCanCleanse + _listDisarmCanCleanse;

            #endregion

            #region Log

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("List Spells Cleanse ASAP: " + ListCleanseASAP);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("List Spells Do Not Cleanse: " + ListCleanseDoNot);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("List Spells Do Not Heal: " + ListHealDoNot);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("List Cast Spells to Interrupt: " + ListCastSpellInterrupt);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("List Channel Spells to Interrupt: " + ListChannelSpellsInterrupt);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListCC: " + ListCC);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListDisarm: " + ListDisarm);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListImmune: " + ListImmune);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListImmuneSpell: " + ListImmuneSpell);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListImmunePhysic: " + ListImmunePhysic);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListRoot: " + ListRoot);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListSilence: " + ListSilence);

            //Logging.Write("");
            //Logging.Write("----------------------------------");
            //Logging.Write("ListSnare: " + ListSnare);

            #endregion
        }

        #endregion

        #region RestRotation

        public Composite RestRotation()
        {
            return new Decorator(
                ret =>
                !Me.IsDead &&
                !Me.IsGhost &&
                !Me.IsCasting &&
                !IsOverrideModeOn &&
                !Me.IsMoving,
                new PrioritySelector(
                    new Decorator(
                        ret =>
                        Me.ManaPercent < THSettings.Instance.SStartEating &&
                        !Me.IsSwimming &&
                        !Me.HasAura("Drink") &&
                        SpellManager.HasSpell("Holy Insight") &&
                        Consumable.GetBestDrink(false) != null,
                        new Action(delegate
                                       {
                                           Thread.Sleep(TimeSpan.FromMilliseconds(300));

                                           if (!Me.HasAura("Drink"))
                                           {
                                               Styx.CommonBot.Rest.DrinkImmediate();
                                               StyxWoW.SleepForLagDuration();
                                           }

                                           if (Me.HasAura("Drink") &&
                                               (Me.ManaPercent <
                                                THSettings.Instance.SDoNotHealAbove ||
                                                ValidUnit(UnitHeal) &&
                                                UnitHeal.HealthPercent < THSettings.Instance.SUrgentHeal) &&
                                               !Me.Combat)
                                           {
                                               return RunStatus.Running;
                                           }
                                           return RunStatus.Success;
                                       }
                            )),
                    new Decorator(
                        ret =>
                        (Me.HealthPercent < THSettings.Instance.SDoNotHealAbove &&
                         Me.CurrentMap.IsBattleground ||
                         Me.HealthPercent < THSettings.Instance.SStartEating) &&
                        Me.ManaPercent > 30 &&
                        !Me.HasAura("Drink") &&
                        !Me.HasAura("Food") &&
                        Consumable.GetBestDrink(false) != null && !CastingorGCDL(),
                        new Action(delegate
                                       {
                                           if (SpellManager.HasSpell("Holy Light") &&
                                               !Me.IsMoving &&
                                               !IsOverrideModeOn)
                                           {
                                               DoNotMove = DateTime.Now + TimeSpan.FromSeconds(3);
                                               Navigator.PlayerMover.MoveStop();
                                               WoWMovement.MoveStop();
                                               CastSpell("Holy Light", Me);
                                               if (DoNotMove < DateTime.Now)
                                               {
                                                   return RunStatus.Running;
                                               }
                                           }

                                           if (SpellManager.HasSpell("Flash of Light") &&
                                               !Me.IsMoving &&
                                               !IsOverrideModeOn &&
                                               Me.ManaPercent > 40)
                                           {
                                               DoNotMove = DateTime.Now + TimeSpan.FromSeconds(1);
                                               Navigator.PlayerMover.MoveStop();
                                               WoWMovement.MoveStop();
                                               CastSpell("Flash of Light", Me);
                                               if (DoNotMove < DateTime.Now)
                                               {
                                                   return RunStatus.Running;
                                               }
                                           }
                                           return RunStatus.Success;
                                       }
                            )),
                    new Decorator(
                        ret =>
                        Me.HealthPercent < THSettings.Instance.SStartEating &&
                        !Me.IsSwimming &&
                        !Me.HasAura("Food") &&
                        Consumable.GetBestFood(false) != null,
                        new Action(delegate
                                       {
                                           Thread.Sleep(TimeSpan.FromMilliseconds(300));

                                           if (!Me.HasAura("Food"))
                                           {
                                               Styx.CommonBot.Rest.FeedImmediate();
                                               StyxWoW.SleepForLagDuration();
                                           }

                                           if (Me.HasAura("Food") &&
                                               Me.HealthPercent <
                                               THSettings.Instance.SDoNotHealAbove &&
                                               !Me.Combat)
                                           {
                                               return RunStatus.Running;
                                           }
                                           return RunStatus.Success;
                                       }
                            ))
                    ));
        }

        #endregion

        #region SafelyFacingTarget

        private void SafelyFacingTarget(WoWUnit unit)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (unit == null)
                {
                    return;
                }

                if (!IsOverrideModeOn && !Me.IsSafelyFacing(unit))
                {
                    unit.Face();
                }
            }
        }

        #endregion

        #region SetAutoAttack

        private Composite SetAutoAttack()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                return new Action(delegate
                                      {
                                          if (Me.Combat && Me.IsAutoAttacking && Me.CurrentTarget != null &&
                                              Me.CurrentTarget.IsValid &&
                                              (Me.CurrentTarget.HasAura("Water Shield") && Me.CurrentTarget.IsPlayer &&
                                               Me.IsFFAPvPFlagged ||
                                               DebuffCCBreakonDamage(Me.CurrentTarget) || Invulnerable(Me.CurrentTarget) ||
                                               !THSettings.Instance.SAutoAttack))
                                          {
                                              //Logging.Write("Stop Attack");
                                              Lua.DoString("RunMacroText('/stopattack');");
                                          }

                                          //Disable this, it conflic with the targeting system
                                          //if (Me.Combat && THSettings.Instance.SAutoAttack && !Me.IsAutoAttacking &&
                                          //    //Me.CurrentTarget != null && Me.CurrentTarget.IsValid &&
                                          //    Attackable(Me.CurrentTarget,5))
                                          //{
                                          //    //Logging.Write("Start Attack");
                                          //    Lua.DoString("RunMacroText('/startattack');");
                                          //}
                                          return RunStatus.Failure;
                                      });
            }
        }

        #endregion

        #region SpellType

        private static readonly HashSet<string> AttackSpells = new HashSet<string>
                                                                   {
                                                                       "Avenger's Shield",
                                                                       "Avenging Wrath",
                                                                       "Crusader Strike",
                                                                       "Consecration",
                                                                       "Divine Storm",
                                                                       "Exorcism",
                                                                       "Hammer of Justice",
                                                                       "Fist of Justice",
                                                                       "Hammer of the Righteous",
                                                                       "Hammer of Wrath",
                                                                       "Holy Wrath",
                                                                       "Inquisition",
                                                                       "Judgment",
                                                                       "Reckoning",
                                                                       "Righteous Fury",
                                                                       "Templar's Verdict",
                                                                       "Turn Evil",
                                                                   };

        private static readonly HashSet<string> DefendSpells = new HashSet<string>
                                                                   {
                                                                       "[Ardent Defender",
                                                                       "Cleanse",
                                                                       "Devotion Aura",
                                                                       "Divine Protection",
                                                                       "Divine Shield",
                                                                       "Emancipate",
                                                                       "Flash of Light",
                                                                       "Hand of Freedom",
                                                                       "Hand of Protection",
                                                                       "Hand of Sacrifice",
                                                                       "Hand of Salvation",
                                                                       "Holy Radiance",
                                                                       "Lay on Hands",
                                                                       "Redemption",
                                                                       "Sacred Shield",
                                                                   };

        private static readonly HashSet<string> SpecialSpells = new HashSet<string>
                                                                    {
                                                                        "Guardian of Ancient Kings",
                                                                        "Holy Avenger",
                                                                        "Holy Prism",
                                                                        "Rebuke",
                                                                    };

        #endregion

        #region TalentSort

        public byte TalentSort(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (target == null)
                {
                    return 0;
                }

                if (!target.IsPlayer)
                {
                    return 0;
                }

                if (target.Class == WoWClass.DeathKnight)
                {
                    return 1;
                }

                if (target.Class == WoWClass.Druid)
                {
                    if (target.Buffs.ContainsKey("Moonkin Form"))
                        return 3;
                    if ((target.Buffs.ContainsKey("Leader of the Pack") &&
                         target.Buffs["Leader of the Pack"].CreatorGuid == target.Guid) ||
                        target.MaxMana < target.MaxHealth/2)
                        return 1;
                    return 4;
                }

                if (target.Class == WoWClass.Hunter)
                {
                    return 2;
                }

                if (target.Class == WoWClass.Mage)
                {
                    return 3;
                }

                if (target.Class == WoWClass.Monk)
                {
                    if (target.MaxMana >= target.MaxHealth/2)
                        return 4;
                    return 1;
                }

                if (target.Class == WoWClass.Paladin)
                {
                    if (target.MaxMana > target.MaxMana/2)
                        return 4;
                    return 1;
                }

                if (target.Class == WoWClass.Priest)
                {
                    if (target.HasAura("Shadowform"))
                        return 3;
                    return 4;
                }

                if (target.Class == WoWClass.Rogue)
                {
                    return 1;
                }

                if (target.Class == WoWClass.Shaman)
                {
                    if (target.MaxMana < target.MaxHealth/2)
                        return 1;
                    if (target.Buffs.ContainsKey("Elemental Oath") &&
                        target.Buffs["Elemental Oath"].CreatorGuid == target.Guid)
                        return 3;
                    return 4;
                }

                if (target.Class == WoWClass.Warlock)
                {
                    return 3;
                }

                if (target.Class == WoWClass.Warrior)
                {
                    return 1;
                }

                return 0;
            }
        }

        public byte TalentSortSimple(WoWUnit target)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                byte sortSimple = TalentSort(target);

                if (sortSimple == 4)
                {
                    return 4;
                }

                if (sortSimple < 4 && sortSimple > 0)
                {
                    return 1;
                }

                return 0;
            }
        }

        #endregion

        #region UpdateStatusEvent

        private void UpdateStatusEvent(object sender, LuaEventArgs args)
        {
            THSettings.Instance.UpdateStatus = true;
        }

        #endregion

        #region OnBotStartedEvent

        private int _bonusJudgementRange;

        private void OnBotStartedEvent(object o)
        {
            if (Me.Inventory.Equipped.Hands != null &&
                Me.Inventory.Equipped.Hands.Name.Contains("Malevolent Gladiator's Scaled Gauntlets"))
            {
                _bonusJudgementRange = 10;
            }
            Logging.Write("----------------------------------");
            Logging.Write("Update Status on Bot Start");
            Logging.Write("----------------------------------");
            THSettings.Instance.SPause = false;
            THSettings.Instance.UpdateStatus = true;
        }

        #endregion

        #region SpecialUnit

        private static readonly HashSet<uint> SpecialUnit = new HashSet<uint>
                                                                {
                                                                    60410, //Elegon
                                                                    60776, //Empyreal Focus
                                                                    60793, //Celestial Protector
                                                                    60913, //Energy Charge
                                                                    60143, //garajal-the-spiritbinder
                                                                    60412, //Empyreal Focus
                                                                    63667, //garalon
                                                                };

        #endregion

        #region UpdateCurrentMap

        //public string CurrentMap;

        //private void UpdateCurrentMapEvent(BotEvents.Player.MapChangedEventArgs args)
        //{
        //    THSettings.Instance.UpdateStatus = true;
        //}

        //private void UpdateCurrentMap()
        //{
        //    if (Me.CurrentMap.IsArena)
        //    {
        //        CurrentMap = "Arena";
        //    }
        //    else if (Me.CurrentMap.IsBattleground && Me.IsFFAPvPFlagged)
        //    {
        //        CurrentMap = "Rated Battleground";
        //    }
        //    else if (Me.CurrentMap.IsBattleground)
        //    {
        //        CurrentMap = "Battleground";
        //    }
        //    else if (Me.CurrentMap.IsDungeon)
        //    {
        //        CurrentMap = "Dungeon";
        //    }
        //    else if (Me.CurrentMap.IsRaid)
        //    {
        //        CurrentMap = "Raid";
        //    }
        //    else
        //    {
        //        CurrentMap = "World";
        //    }

        //    Logging.Write("----------------------------------");
        //    Logging.Write("CurrentMap: " + CurrentMap);
        //    Logging.Write("----------------------------------");
        //}

        #endregion

        #region UpdateGroupHealingMembers

        private void UpdateGroupChangeEvent(object sender, LuaEventArgs args)
        {
            if (IsGroupHealingOn())
            {
                Logging.Write("Update Selective Group Healing on Group Member Change");
                THSettings.Instance.UpdateStatus = true;
            }
        }

        public List<WoWPlayer> GroupHealingMembers = new List<WoWPlayer>();

        private void UpdateGroupHealingMembers()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                GroupHealingMembers.Clear();
                //Logging.Write("Debug " + GroupHealingMembers.Count() + " Members");

                if (IsGroupHealingOn())
                {
                    foreach (var woWPartyMember in GroupMembers)
                    {
                        if (woWPartyMember.ToPlayer() != null)
                        {
                            if (THSettings.Instance.SGroup1 && woWPartyMember.GroupNumber == 0 ||
                                THSettings.Instance.SGroup2 && woWPartyMember.GroupNumber == 1 ||
                                THSettings.Instance.SGroup3 && woWPartyMember.GroupNumber == 2 ||
                                THSettings.Instance.SGroup4 && woWPartyMember.GroupNumber == 3 ||
                                THSettings.Instance.SGroup5 && woWPartyMember.GroupNumber == 4 ||
                                THSettings.Instance.SGroup6 && woWPartyMember.GroupNumber == 5 ||
                                THSettings.Instance.SGroup7 && woWPartyMember.GroupNumber == 6 ||
                                THSettings.Instance.SGroup8 && woWPartyMember.GroupNumber == 7)
                            {
                                Logging.Write("Add " + woWPartyMember.ToPlayer().Class + " in Group: " +
                                              (Convert.ToByte(woWPartyMember.GroupNumber) + 1) +
                                              " to Selective Group Healing");
                                GroupHealingMembers.Add(woWPartyMember.ToPlayer());
                            }
                        }
                    }

                    if (GroupHealingMembers.Any())
                    {
                        Logging.Write("----------------------------------");
                        Logging.Write("You are assigned to Heal " + GroupHealingMembers.Count() + " Members");
                        Logging.Write("You will also heal your Target, your Focus and Yourself!");
                        Logging.Write("----------------------------------");
                    }
                }
            }
        }

        #endregion

        #region UpdateMyLatency

        private static double _myLatency;

        private static void UpdateMyLatency()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (THSettings.Instance.SLagTolerance)
                {
                    //If SLagTolerance enabled, start casting next spell MyLatency Millisecond before GlobalCooldown ready.

                    _myLatency = (StyxWoW.WoWClient.Latency);

                    //Use here because Lag Tolerance cap at 400
                    //Logging.Write("----------------------------------");
                    //Logging.Write("MyLatency: " + MyLatency);
                    //Logging.Write("----------------------------------");

                    if (_myLatency > 400)
                    {
                        //Lag Tolerance cap at 400
                        _myLatency = 400;
                    }
                }
                else
                {
                    _myLatency = 0;
                }
            }
        }

        #endregion

        #region UpdateMyTalent

        private static string _hasTalent = "";

        private void UpdateMyTalentEvent(object sender, LuaEventArgs args)
        {
            UpdateMyTalent();
        }

        private void UpdateMyTalent()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                _hasTalent = "";
                for (int i = 1; i <= 18; i++)
                {
                    _hasTalent = _hasTalent +
                                 Lua.GetReturnVal<String>(
                                     string.Format(
                                         "local t= select(5,GetTalentInfo({0})) if t == true then return '['..select(1,GetTalentInfo({0}))..'] ' end return nil",
                                         i), 0);
                }

                Logging.Write("----------------------------------");
                Logging.Write("Talent:");
                Logging.Write(_hasTalent);
                Logging.Write("----------------------------------");
                _hasTalent = "";
            }
        }

        #endregion

        #region UseHealthstone

        private Composite UseHealthstone()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                return
                    new Decorator(
                        ret =>
                        THSettings.Instance.SHealthStone != 0 &&
                        Me.Combat &&
                        Me.HealthPercent < THSettings.Instance.SHealthStone &&
                        Me.CurrentTarget != null &&
                        Me.CurrentTarget.CurrentHealth > Me.HealthPercent,
                        new Action(delegate
                                       {
                                           WoWItem hs = Me.BagItems.FirstOrDefault(o => o.Entry == 5512);
                                           //5512 Healthstone
                                           if (hs != null && hs.CooldownTimeLeft.TotalMilliseconds <= _myLatency)
                                           {
                                               hs.Use();
                                               //Lua.DoString("RunMacroText(\"/s Used Healthstone\")");
                                               Logging.Write("Use Healthstone at " + Me.HealthPercent + "%");
                                           }
                                           return RunStatus.Failure;
                                       })
                        );
            }
        }

        #endregion

        #region UpdateCooldown

        //private DateTime _sacredShieldCooldown;
        //private DateTime _judgmentCooldown;
        private DateTime _holyShockCooldown;

        private void UpdateCooldown(string spellName)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                //switch (spellName)
                //{
                //        //case "Sacred Shield":
                //        //    _sacredShieldCooldown = DateTime.Now + SpellManager.Spells[spellName].CooldownTimeLeft;
                //        //    break;
                //        //case "Judgment":
                //        //    _judgmentCooldown = DateTime.Now + SpellManager.Spells[spellName].CooldownTimeLeft;
                //        //    break;
                //    case "Holy Shock":
                //        _holyShockCooldown = DateTime.Now + GetSpellCooldown(spellName);
                //        break;
                //}
            }
        }

        #endregion

        #region UpdateMyNoGCDSpell

        private static readonly HashSet<string> NoGCDSpells = new HashSet<string>();

        private void UpdateMyNoGCDSpell()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (SpellManager.HasSpell("Arcane Torrent"))
                {
                    NoGCDSpells.Add("Arcane Torrent");
                }

                if (SpellManager.HasSpell("Ardent Defender"))
                {
                    NoGCDSpells.Add("Ardent Defender");
                }

                if (SpellManager.HasSpell("Avenging Wrath"))
                {
                    NoGCDSpells.Add("Avenging Wrath");
                }

                if (HasGlyph.Contains("63218"))
                {
                    NoGCDSpells.Add("Beacon of Light");
                }

                if (SpellManager.HasSpell("Devotion Aura"))
                {
                    NoGCDSpells.Add("Devotion Aura");
                }

                if (SpellManager.HasSpell("Divine Favor"))
                {
                    NoGCDSpells.Add("Divine Favor");
                }

                if (SpellManager.HasSpell("Divine Protection"))
                {
                    NoGCDSpells.Add("Divine Protection");
                }

                if (SpellManager.HasSpell("Guardian of Ancient Kings"))
                {
                    NoGCDSpells.Add("Guardian of Ancient Kings");
                }

                if (SpellManager.HasSpell("Holy Avenger"))
                {
                    NoGCDSpells.Add("Holy Avenger");
                }

                if (SpellManager.HasSpell("Lay on Hands"))
                {
                    NoGCDSpells.Add("Lay on Hands");
                }

                if (SpellManager.HasSpell("Rebuke"))
                {
                    NoGCDSpells.Add("Rebuke");
                }

                if (SpellManager.HasSpell("Reckoning"))
                {
                    NoGCDSpells.Add("Reckoning");
                }


                Logging.Write("----------------------------------");
                Logging.Write("Spells that don't use Global Cooldown:");
                foreach (string spell in NoGCDSpells)
                {
                    Logging.Write(" -" + spell);
                }
                Logging.Write("----------------------------------");
            }
        }

        #endregion

        #region UpdateMyGlyph

        private void UpdateMyGlyphEvent(object sender, LuaEventArgs args)
        {
            UpdateMyGlyph();
        }

        //private static readonly HashSet<string> NoGCDSpells = new HashSet<string> { };
        public string HasGlyph;
        public string HasGlyphName;

        private void UpdateMyGlyph()
        {
            HasGlyph = "";
            HasGlyphName = "";
            //using (StyxWoW.Memory.AcquireFrame())
            {
                var glyphCount = Lua.GetReturnVal<int>("return GetNumGlyphSockets()", 0);

                if (glyphCount != 0)
                {
                    for (int i = 1; i <= glyphCount; i++)
                    {
                        string lua =
                            String.Format(
                                "local enabled, glyphType, glyphTooltipIndex, glyphSpellID, icon = GetGlyphSocketInfo({0});if (enabled) then return glyphSpellID else return 0 end",
                                i);

                        var glyphSpellId = Lua.GetReturnVal<int>(lua, 0);

                        try
                        {
                            if (glyphSpellId > 0)
                            {
                                HasGlyphName = HasGlyphName + "[" + (WoWSpell.FromId(glyphSpellId)) + " - " +
                                               glyphSpellId +
                                               "] ";
                                HasGlyph = HasGlyph + "[" + glyphSpellId + "] ";
                            }
                            else
                            {
                                Logging.Write("Glyphdetection - No Glyph in slot " + i);
                                //TreeRoot.Stop();
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.Write("We couldn't detect your Glyphs");
                            Logging.Write("Report this message to us: " + ex);
                            //TreeRoot.Stop();
                        }
                    }
                }

                Logging.Write("----------------------------------");
                Logging.Write("Glyph:");
                Logging.Write(HasGlyphName);
                Logging.Write("----------------------------------");
            }
        }

        #endregion

        #region UpdateStatus

        private void UpdateStatus()
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (THSettings.Instance.UpdateStatus)
                    if (Me.IsValid)
                    {
                        {
                            //Reset BurstLast on start
                            BurstLast = DateTime.Now;

                            if (THSettings.Instance.SLastSavedSpec != THForm.GetCurrentSpec())
                            {
                                Logging.Write("----------------------------------");
                                Logging.Write("You just change specialization to " + THForm.GetCurrentSpec());
                                Logging.Write("Please select the appropriate settings");
                                Logging.Write("----------------------------------");
                                OnButtonPress();
                            }

                            Logging.Write("----------------------------------");
                            Logging.Write("Building Rotation base on current Talents and Glyphs......");
                            Logging.Write("----------------------------------");
                            Logging.Write("");

                            //if (TreeRoot.Current.Name != "Questing" && 
                            //    TreeRoot.Current.Name != "Grind Bot" &&
                            //    TreeRoot.Current.Name != "LazyRaider" && 
                            //    TreeRoot.Current.Name != "BGBuddy" &&
                            //    TreeRoot.Current.Name != "DungeonBuddy" && 
                            //    TreeRoot.Current.Name != "Mixed Mode" && 
                            //    GetAsyncKeyState(Keys.F10) == 0)
                            //{
                            //    Logging.Write("This CC only work with DungeonBuddy - LazyRaider - Questing - Grind Bot.");
                            //    Logging.Write("Custom Class Stop.");

                            //    TreeRoot.Stop();
                            //}

                            if (TreeRoot.Current.Name == "LazyRaider" && !THSettings.Instance.SMoveToTarget)
                            {
                                THSettings.Instance.SMoveToTarget = false;
                                //Logging.Write(LogLevel.Diagnostic,
                                //              "EnableMovement: " + THSettings.Instance.EnableMovement);
                            }
                            //else
                            //{
                            //    THSettings.Instance.SMoveToTarget = true;
                            //    THSettings.Instance.SAutoTarget = true;
                            //Logging.Write(LogLevel.Diagnostic,
                            //              "EnableMovement: " + THSettings.Instance.EnableMovement);
                            //}

                            //if (Me.Level < 85)
                            //{
                            //    Logging.Write("This CC only work on Level 85-90.");
                            //    //TreeRoot.Stop();
                            //    return;
                            //}


                            ReadXML();

                            UpdateMyTalent();

                            UpdateMyGlyph();

                            UpdateMyNoGCDSpell();

                            UpdateMyLatency();

                            UpdateGroupHealingMembers();

                            _gcdReady = DateTime.Now;
                            Logging.Write("----------------------------------");
                            Logging.Write("Building Rotation Completed");
                            Logging.Write("----------------------------------");
                            Logging.Write("");

                            Logging.Write("----------------------------------");
                            Logging.Write("Press Control + " + IndexToKeys(THSettings.Instance.SPauseKey) +
                                          " To Toggle Pause Mode.");
                            Logging.Write("----------------------------------");
                            Logging.Write("");

                            Logging.Write("----------------------------------");
                            Logging.Write("Press Control + " + IndexToKeys(THSettings.Instance.SAOEKey) +
                                          " To Toggle AoE Mode");
                            Logging.Write("----------------------------------");
                            Logging.Write("");

                            if (THSettings.Instance.SBurstKey > 7)
                            {
                                Logging.Write("----------------------------------");
                                Logging.Write("Press Control + " + IndexToKeys(THSettings.Instance.SBurstKey - 7) +
                                              " To Toggle Burst Mode");
                                Logging.Write("----------------------------------");
                                Logging.Write("");
                            }
                            THSettings.Instance.UpdateStatus = false;
                        }
                    }
            }
        }

        #endregion

        #region ValidUnit

        private bool ValidUnit(WoWUnit u)
        {
            //using (StyxWoW.Memory.AcquireFrame())
            {
                if (u == null || !u.IsValid || !u.Attackable || !u.IsAlive || !u.CanSelect || u.IsDead ||
                    u.IsCritter && (u.CurrentTarget == null || u.CurrentTarget != null && u.CurrentTarget != Me) ||
                    u.IsNonCombatPet) // || Blacklist.Contains(u.Guid))
                {
                    return false;
                }
                return true;
            }
        }

        #endregion
    }
}